<?php


namespace app\admin\controller;

use think\admin\Controller;
use think\facade\View;

use think\Db;

/**
 * 地区设置
 * Class City
 * @package app\admin\controller
 */
class City extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'lg_citys';

    /**
     * 地区列表
     * @auth true
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        //标题
        $this->title = '地区管理';
        //链接数据库
        $query = $this->_query($this->table)->where(array('level_type'=>1))->select();
        $query->page();
    }

    public function test(){
        $id = input('id');

        $info = $this->app->db->table($this->table)->where('parent_id',$id)->find();
        if($info){
            $where['id'] = $info['parent_id'];
            $res = $this->app->db->table($this->table)->where($where)->select()->toArray();
            echo $this->app->db->table($this->table)->getLastSql();
            dump($res);exit;
        }

        dump(11111);exit;

    }

    public function getCityList(){
        $level_type = input('level_type',2);
        $parent_id = input('parent_id',450000);
        $where['level_type'] = $level_type;
        $where['parent_id'] = $parent_id;
        $res = $this->app->db->name($this->table)->where($where)->select();
        if($res){
            $row['code'] = 200;
            $row['data'] = $res;
            $row['msg'] = '';
        }else{
            $row['code'] = 400;
            $row['msg'] = "暂无数据";
        }
        echo json_encode($row);
    }

//    protected function _page_filter(&$list)
//    {
//        foreach ($list as $key => $value) {
//            $value['type'] = $this->type[$value['type']];
//            $value['status'] = $this->status[$value['status']];
//            $list[$key] = $value;
//        }
//    }

    /**
     * @return string
     * 获取上级省市区
     */
    public function getCity(){
        if(request()->isAjax()){
            $cate = input('ccate')-1;
            $id = input('id');
            $p_channel = $this->app->db->name($this->table)->where(['level_type'=>$cate])->select();
            $op = "<option value=''>-- 请选择 --</option>";
            foreach ($p_channel as $v) {
                $v['merger_name'] = str_replace('中国,','',$v['merger_name']);

                if($id == $v['id']){
                    $op .= "<option value='{$v['id']}' selected>{$v['merger_name']}</option>";
                }else{
                    $op .= "<option value='{$v['id']}'>{$v['merger_name']}</option>";
                }
            }
            return $op;
        }
    }

    public function add()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑省
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_applyFormToken();
        //门店
        $id = input('id');
        $info = $this->app->db->table($this->table)->where('id',$id)->field('id,name,parent_id,level_type,lng,lat')->find();
        if($info){
            $merger_name = $this->app->db->table($this->table)->where('id',$info['parent_id'])->field('name')->find();
            $info['merger_name'] = $merger_name['name'];
        }else{
            $info['merger_name'] = '';
        }
        $this->assign('info', $info);
        $this->_form($this->table, 'form');
    }

    /**
     * 编辑城市
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function editCity(){
//        if(request()->isAjax()){
            $data=input();

            $maxId=$this->app->db->name($this->table)->order('id desc')->value('id');
            if($data['level_type']==1){
                $merger_name = '中国,'.$data['name'];
            }

            if($data['level_type']==2){
                $faData=$this->app->db->name($this->table)->find($data['parent_id']);
                $merger_name = '中国,'.$faData['name'].','.$data['name'];
            }

            if($data['level_type']==3){
                $faData=$this->app->db->name($this->table)->find($data['parent_id']);
                $ffData=$this->app->db->name($this->table)->find($faData['parent_id']);
                $merger_name = '中国,'.$ffData['name'].','.$faData['name'].','.$data['name'];
            }


            $sqlData['name']=$data['name'];
            $sqlData['parent_id']=$data['parent_id'];
            $sqlData['short_name']=$data['name'];
            $sqlData['level_type']=$data['level_type'];
            $sqlData['city_code']='';
            $sqlData['zip_code']='';
            $sqlData['merger_name']= $merger_name;
            $sqlData['lng']=$data['lng'];
            $sqlData['lat']=$data['lat'];
            $sqlData['pinyin']='';
            $sqlData['can_be_del']=1;

            if(isset($data['id']) && !empty($data['id'])){
                $res = $this->app->db->name($this->table)->where('id', $data['id'])->update($sqlData);
                sysoplog('地区管理', '编辑数据成功');
            }else{
                $sqlData['id']=$maxId+1;
                $res = $this->app->db->name($this->table)->insert($sqlData);
                sysoplog('地区管理', '新增数据成功');
            }

            if($res){
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }

//        }
    }

    /**
     * 删除
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_applyFormToken();
        $id = input('id');

        $info = $this->app->db->table($this->table)->where('parent_id',$id)->find();
        if($info){
            $this->error('操作失败,存在子级地区');
        }else{
            $this->_delete($this->table);
            sysoplog('地区管理', '删除数据成功');
        }

    }

    /**
     * 树形结构
     * @login true
     */
    public function tree()
    {
        $this->title = '地域列表';
        $this->fetch();
    }

    /**
     * 获取数据
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTreeData()
    {
        $list = $this->app->db->name($this->table)
            ->where('id', '<>', 100000)
            ->cache(true, 3600)
            ->select()->toArray();
        return json($list);
    }

}